package com.xh.one2one.lesson.service.impl;

import com.xh.one2one.lesson.bean.request.LessonEntranceExamRequest;
import com.xh.one2one.lesson.model.LessonEntranceExam;
import com.xh.one2one.lesson.dao.LessonEntranceExamMapper;
import com.xh.one2one.lesson.service.ILessonEntranceExamService;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.core.bean.XhPage;
import com.xh.core.util.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
/**
 * <p>
 * 一对一备课进门考 服务实现类
 * </p>
 *
 * @author TW
 * @since 2021-04-14
 */
@Service
public class LessonEntranceExamServiceImpl extends BaseServiceImpl<LessonEntranceExamMapper, LessonEntranceExam> implements ILessonEntranceExamService {
    @Override
    public XhPage queryLessonEntranceExamPage(XhPage page, LessonEntranceExamRequest bean) {
        QueryWrapper qw=new QueryWrapper<LessonEntranceExam>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List queryLessonEntranceExam(LessonEntranceExamRequest bean) {
        QueryWrapper qw=new QueryWrapper<LessonEntranceExam>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return this.dao.selectList(qw);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByRequestBean(LessonEntranceExamRequest bean) {
        QueryWrapper<LessonEntranceExam> queryWrapper = new QueryWrapper<>();
        prepareParams(bean,queryWrapper);
        deleteByCondition(queryWrapper);
    }


    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(LessonEntranceExamRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        if(!StringUtils.isEmpty(bean.getLessonTimeId())){
            qw.eq("lesson_time_id",bean.getLessonTimeId());
        }
        return qw;
    }
}
